﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Interactivity = System.Windows.Interactivity;
namespace XamlingCore.WP.Controls.Behavior
{
    public class TextBoxKeyUpUpdateBehaviour : Interactivity.Behavior<TextBox>
    {
        protected override void OnAttached()
        {
            base.OnAttached();

            AssociatedObject.KeyUp += AssociatedObject_KeyUp;

        }

        void AssociatedObject_KeyUp(object sender, KeyEventArgs e)
        {
            var bindingExpression = AssociatedObject.GetBindingExpression(TextBox.TextProperty);

            if (bindingExpression != null)
            {
                bindingExpression.UpdateSource();
            }
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();

            AssociatedObject.KeyUp -= AssociatedObject_KeyUp;
        }
    }
}
